/**
 * Created by chenyu on 17/10/25.
 */
var fs = require('fs');
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
    host: 'localhost:9200',
    log: 'trace'
});

var timeArr = [];

function getTimeStamp() {
    var files = fs.readdirSync('./data/result');
    for (var i = 0; i < files.length; i++) {
        var filePath = './data/result/' + files[i];
        var data = fs.readFileSync(filePath);
        var line = data.toString().split('\n');
        for (var j = 0; j < line.length; j++) {
            if (line[j] != '') {
                var dataLine = JSON.parse(line[j]);
                timeArr.push(dataLine);
            }
        }
    }
}
getTimeStamp();
console.log(timeArr)
function saveDate() {
    var bulk = [];

    for (var i = 0; i < timeArr.length; i++) {
        var index = { index:  { _index: 'bigship', _type: 'history' } }
        bulk.push(index)
        bulk.push(timeArr[i])
    }

    client.bulk({
        body: bulk
    }, function (err, resp) {
        console.log(resp)
    });

}
saveDate();
// var http = require("http");
//
// var options = {
//     "method": "POST",
//     "hostname": "localhost",
//     "port": "9200",
//     "path": "/bigship/history/_search",
//     "headers": {
//         "content-type": "application/json",
//         "cache-control": "no-cache",
//         "postman-token": "ffb13b38-132e-864d-2228-3fee1f248017"
//     }
// };
//
// var req = http.request(options, function (res) {
//     var chunks = [];
//
//     res.on("data", function (chunk) {
//         chunks.push(chunk);
//     });
//
//     res.on("end", function () {
//         var body = Buffer.concat(chunks);
//         console.log(JSON.parse(body.toString()).hits.hits.length);
//
//     });
// });
//
// req.write(JSON.stringify({ query: { range: { timestamp: { gte: 1, lte: 1571882803211 } } }, size: 10000}));
// req.end();


// client.delete({
//     index: 'test',
//     type: 'history',
//     id: '1'
// }, function (error, response) {
//     console.log(response)
// });


